<?php

use Workerman\Connection\AsyncUdpConnection;
//use Workerman\Worker;
require_once __DIR__.'/../vendor/autoload.php';

$worker_udp=new \Workerman\Worker('tcp://0.0.0.0:4002');

//$worker_udp->onWorkerStart=function($woker_udp){
//        \Workerman\Lib\Timer::add(3, function()
//        {
//            $shell="ffmpeg -re -i tcp://127.0.0.1:4002 -an -vcodec copy -f flv rtmp://106.75.165.88:8298/live/123";
//            exec($shell,$res);
//        },'',false);
//};

$client=[];
//$fp=fopen('1.mp4','w');
global $client;
//global $fp;
$worker_udp->onConnect=function($connection){
    global $client;
    if(!in_array($connection,$client)){
        $client[]=$connection;
    }
};
$worker_udp->onMessage=function ($connection,$msg){
    global $client;
    foreach ($client as $c){
        if($c!==$connection){
            $len = $c->send($msg);
//            global $fp;
//            fwrite($fp,$msg);
            echo count($client);
            echo $len;
            echo '===='.PHP_EOL;
        }
    }
};
$worker_udp->onBufferFull = function($connection)
{
    echo "bufferFull and do not send again\n";
};
$worker_udp->onBufferDrain = function($connection)
{
    echo "buffer drain and continue send\n";
};
$worker_udp->onError = function($connection, $code, $msg)
{
    echo "error $code $msg\n";
};
$worker_udp->onClose = function($connection)
{
    echo "connection closed\n";
};
if (!defined('GLOBAL_START')) {
    \Workerman\Worker::runAll();
}

